﻿#include <QMessageBox>
#include <QMenu>
#include "common.h"
#include "../DatabaseMgmt/GoodsKind.h"
#include "../DatabaseMgmt/GoodsStandard.h"
#include "../DatabaseMgmt/AppraisalBasis.h"
#include "PageBasisSettings.h"
#include "ui_PageBasisSettings.h"



CPageBasisSettings::CPageBasisSettings(QWidget* parent /*= 0*/, Qt::WindowFlags f /*= 0*/)
	: QWidget(parent, f)
	, m_ui(nullptr)
{
	init();
}

CPageBasisSettings::~CPageBasisSettings()
{

}

void CPageBasisSettings::init()
{
	m_ui = new Ui::PageBasisSettings();
	m_ui->setupUi(this);

	m_ui->twWholeStandard->setColumnWidth(0, 300);
	m_ui->twWholeStandard->setColumnWidth(1, 200);
	m_ui->twWholeStandard->setColumnWidth(2, 200);
	m_ui->twWholeStandard->setColumnWidth(3, 200);
	m_ui->twWholeStandard->setColumnWidth(4, 200);

	initLawesList();

	updateMouldKind();

	updateWholeStandardInfo();

	connect(m_ui->btnAddKind, SIGNAL(clicked()), this, SLOT(slotAddKind()));
	connect(m_ui->btnAddMainly, SIGNAL(clicked()), this, SLOT(slotAddMainly()));
	connect(m_ui->twWholeStandard, SIGNAL(customContextMenuRequested(const QPoint&)),
		this, SLOT(slotStandardRightMenu(const QPoint&)));

	//connect(m_ui->btnAddMinor, SIGNAL(clicked()), this, SLOT(slotAddMinor()));
	connect(m_ui->btnHome, SIGNAL(clicked()), this, SIGNAL(sigHome()));
	connect(m_ui->btnAddLaw, SIGNAL(clicked()), this, SLOT(slotAddLaw()));
	connect(m_ui->btnModifyLaw, SIGNAL(clicked()), this, SLOT(slotModifyLaw()));
	connect(m_ui->lwLawes, SIGNAL(customContextMenuRequested(const QPoint&)),
		this, SLOT(slotRemoveLawRightMenu(const QPoint&)));
	connect(m_ui->btnRefresh, SIGNAL(clicked()), this, SLOT(slotRefreshLaw()));

}


void CPageBasisSettings::initLawesList()
{
	CAppraisalBasis appraisal;

	QVector<LawBasis> lawes;
	int ret = appraisal.queryAll(lawes);
	if (CONST_ERR_SUCCESS != ret)
	{
		return;
	}

	m_ui->lwLawes->clear();

	for (int i = 0; i < lawes.size(); ++i)
	{
		QListWidgetItem* pItem = new QListWidgetItem(lawes[i].law);
		pItem->setData(Qt::UserRole, lawes[i].uid);
		pItem->setCheckState(Qt::Checked);

		m_ui->lwLawes->addItem(pItem);
	}
}

void CPageBasisSettings::slotAddKind()
{
	if (m_ui->leKindName->text().isEmpty())
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("类别模板名称不能为空！"));
		return;
	}
	CGoodsKind kind;

	GoodsKind stInfo;
	stInfo.name = m_ui->leKindName->text();

	kind.insertRow(stInfo);

	m_ui->leKindName->clear();
}

void CPageBasisSettings::slotAddMainly()
{
	if (m_ui->leMainlyName->text().isEmpty())
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("主要元素名称不能为空！"));
		return;
	}
	CGoodsStandard stand;

	GoodsStandard info;
	info.kind_id = m_ui->cmbMainly->currentData().toUInt();
	info.name = m_ui->leMainlyName->text();
	info.min_value = m_ui->dspbMainlyMin->value();
	info.max_value = m_ui->dspbMainlyMax->value();
	info.mainly = DEF_TTRP_YES;

	stand.insertRow(info);

	updateWholeStandardInfo();
}

void CPageBasisSettings::slotStandardRightMenu(const QPoint& pos)
{
	Q_UNUSED(pos);
	QMenu menu(this);

	menu.addAction(QStringLiteral("删除"), this, SLOT(slotRemoveStandardTriggered(bool)));

	menu.exec(QCursor::pos());
}

void CPageBasisSettings::slotRemoveStandardTriggered(bool checked /*= false*/)
{
	Q_UNUSED(checked);
	QTreeWidgetItem* pItem = m_ui->twWholeStandard->currentItem();

	if (nullptr == pItem)
	{
		return;
	}
	if (pItem->parent() == nullptr)
	{
		qInfo() << QStringLiteral("不能删除元素类别！");
		return;
	}
	CViewWholeStandard view_standard;
	uint32_t uid = pItem->data(0, Qt::UserRole).toUInt();
	view_standard.removeById(uid);

	updateWholeStandardInfo();
}

// void CPageBasisSettings::slotAddMinor()
// {
// 	if (m_ui->leMinorName->text().isEmpty())
// 	{
// 		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("次要元素名称不能为空！"));
// 		return;
// 	}
// 	CGoodsStandard stand;
// 
// 	GoodsStandard info;
// 	info.kind_id = m_ui->cmbMinor->currentData().toUInt();
// 	info.name = m_ui->leMainlyName->text();
// 	info.min_value = m_ui->dspbMainlyMin->value();
// 	info.max_value = m_ui->dspbMainlyMax->value();
// 	info.mainly = DEF_TTRP_NO;
// 
// 	stand.insertRow(info);
// 
// 	updateWholeStandardInfo();
// }

void CPageBasisSettings::slotAddLaw()
{
	QString strText = m_ui->leLawName->text();
	if (strText.trimmed().isEmpty())
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("法律或规定依据不能为空！"));
		return;
	}
	CAppraisalBasis basis;
	int ret = basis.insertRow(strText);

	if (ret != CONST_ERR_SUCCESS)
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("添加法律规定失败！"));
		return;
	}
	initLawesList();
}

void CPageBasisSettings::slotModifyLaw()
{
	QListWidgetItem* pItem = m_ui->lwLawes->currentItem();
	if (nullptr == pItem)
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("未选择要修改的法律规定！"));
		return;
	}

	QString strText = m_ui->leLawName->text();
	if (strText.trimmed().isEmpty())
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("法律或规定依据不能为空！"));
		return;
	}
	CAppraisalBasis basis;
	int ret = basis.modifyRow(pItem->data(Qt::UserRole).toUInt(),strText);

	if (ret != CONST_ERR_SUCCESS)
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("修改法律规定失败！"));
		return;
	}
	initLawesList();
}

void CPageBasisSettings::slotRemoveLawTriggered(bool checked /*= false*/)
{
	Q_UNUSED(checked);
	QListWidgetItem* pItem = m_ui->lwLawes->currentItem();
	if (nullptr == pItem)
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("未选择要删除的法律规定！"));
		return;
	}
	CAppraisalBasis basis;
	int ret = basis.removeRow(pItem->data(Qt::UserRole).toUInt());

	if (ret != CONST_ERR_SUCCESS)
	{
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("删除法律规定失败！"));
		return;
	}
	initLawesList();
}

void CPageBasisSettings::slotRemoveLawRightMenu(const QPoint& pos)
{
	Q_UNUSED(pos);
	QMenu menu(this);

	menu.addAction(QStringLiteral("删除"), this, SLOT(slotRemoveLawTriggered(bool)));

	menu.exec(QCursor::pos());
}

void CPageBasisSettings::slotRefreshLaw()
{
	initLawesList();
}

void CPageBasisSettings::updateWholeStandardInfo()
{
	CViewWholeStandard view_standard;

	QVector<ViewWholeStandard> standards;

	view_standard.queryAll(standards);

	initStanardTreeWidget(standards);
}

void CPageBasisSettings::initStanardTreeWidget(const QVector<ViewWholeStandard>& standards)
{
	m_ui->twWholeStandard->clear();

	QTreeWidgetItem* pTopItem = nullptr;

	for (int i = 0; i < standards.size(); ++i)
	{
		ViewWholeStandard stand = standards[i];
		if (pTopItem == nullptr)
		{
			pTopItem = new QTreeWidgetItem(m_ui->twWholeStandard);
			pTopItem->setText(0, stand.kind_name);
			pTopItem->setData(0, Qt::UserRole, stand.kind_id);
		}
		else
		{
			if (pTopItem->data(0, Qt::UserRole).toUInt() != stand.kind_id)
			{
				pTopItem = new QTreeWidgetItem(m_ui->twWholeStandard);
				pTopItem->setText(0, stand.kind_name);
				pTopItem->setData(0, Qt::UserRole, stand.kind_id);
			}
		}

		QTreeWidgetItem* pItem = new QTreeWidgetItem(pTopItem);
		pItem->setText(0, stand.standard_name);
		pItem->setData(0, Qt::UserRole, stand.standard_id);
		pItem->setText(1, stand.standard_name);
		pItem->setText(2, QString::number(stand.standard_min_value, 'f', 4));
		pItem->setText(3, QString::number(stand.standard_max_value, 'f', 4));
		pItem->setData(4, Qt::UserRole, stand.standard_mainly);
		if (stand.standard_mainly == DEF_TTRP_YES)
		{
			pItem->setText(4, QStringLiteral("主"));
		}
		else if (stand.standard_mainly == DEF_TTRP_NO)
		{
			pItem->setText(4, QStringLiteral("次"));
		}
		else
		{
			pItem->setText(4, QStringLiteral("未知"));
		}
	}

	m_ui->twWholeStandard->expandAll();
}

void CPageBasisSettings::updateMouldKind()
{
	CGoodsKind kind;

	QVector<GoodsKind> infos;

	kind.queryAll(infos);

	for (int i = 0; i < infos.size(); ++i)
	{
		GoodsKind item = infos[i];

		m_ui->cmbMainly->addItem(item.name, item.uid);
		//m_ui->cmbMinor->addItem(item.name, item.uid);
	}
}
